package com.jdbcpool;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Calendar;
import java.util.HashMap;

public class PreparedStatementProxy implements InvocationHandler {
	private PreparedStatement pstmt;
	private String sql;
	private JdbcPoolDataSource ds;


	PreparedStatementProxy(PreparedStatement target, JdbcPoolDataSource ds) {
		pstmt = target;
		setDs(ds);
	}

	public Object invoke(Object proxy, Method method, Object[] args)
			throws Throwable {
		try {
			if (method.getName().equals("executeQuery")
					|| method.getName().equals("execute")
					|| method.getName().equals("executeUpdate")) {
				ds.getPoolLog().log("execute sql:"+sql, PoolLog.DEBUG_LEVEL);
				return method.invoke(pstmt, args);
			} else {
				return method.invoke(pstmt, args);
			}
		} catch (Exception e) {
			if ( e.getCause() ==null) {
				throw e;
			}else{
				throw e.getCause();
			}
		}
	}

	public JdbcPoolDataSource getDs() {
		return ds;
	}

	public void setDs(JdbcPoolDataSource ds) {
		this.ds = ds;
	}

	public String getSql() {
		return sql;
	}

	public void setSql(String sql) {
		this.sql = sql;
	}


}
